<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\UserInsertRequest;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BlockController extends Controller
{
    //显示板块列表
    public function getIndex($id = 1 )
    {
       //获取参数列表
       // $blocks = DB::table('block')->where('sid',$id)->get();
       $blocks = DB::select("select binfo,bid,bname,bnum,buid,bintro,username,sname from block left join users on block.buid=users.uid  where sid = " .$id);

       // dd($blocks);
        //引入列表模版  并显示列表内容
        return view('admin.block.index',['blocks'=>$blocks]);
    }

    //添加板块
    public function getAdd()
    {
        $user = DB::table('users')->get();
       
        //解析添加用户模版
    	return view('admin.block.add',['users'=>$user]);
    }

    //执行添加板块
    public function postInsert(Request $request)
    {
        $brr = [1=>'NBA区',2=>'交易区',3=>'技术区'];
        //提取数据
        $data = $request->only(['sid','bname','bintro','buid','binfo']);
        $data['sname'] = $brr[$request->input('sid')];
        // dd($data);
        //插入数据
        $resb = DB::table('block')->insert($data);
        $resu = DB::update('update users set userauth = 2 where uid = ?',[$data['buid']]);
        if ($resb || $resu) {
            //执行成功
            return redirect('/admin/block/index')->with('success','添加成功');
        }else{
            //执行失败
            return back()->with('error','添加失败'); 
        }
    }



    //删除
    public function postDelete(Request $request)
    {
        //获取id值
        $id = $request->input('bid');
        //查询是否有帖子
        $ress = DB::table('block')->where('bid',$id)->value('bnum');
        if ($ress) {
            echo 1;
        }else{
            //执行删除
            $res = DB::table('block')->where('bid',$id)->delete();
           echo 0;
        }
        // echo $res;
    }
    //显示修改页面
    public function getEdit($id)
    {
        //查询用户
         $b = DB::select("select binfo,bid,bname,bnum,buid,sid,bintro,username,uid from block left join users on block.buid=users.uid  where bid = " .$id)[0];
        //查找所有用户
         $user = DB::select("select uid,username from users");
        // dd($b);
        //解析模版
        return view('/admin/block/edit',['block'=>$b,'user'=>$user]);
    }
   
    //处理修改信息
    public function postUpdate(Request $request)
    {
        //获取信息
        $data = $request->except(['_token']);
       
        // dd($data);
       
        //执行修改板块操作
            $resb = DB::table('block')->where('bid',$data['bid'])->update($data);
        //执行修改用户表操作
            $resu = DB::update('update users set userauth = 2 where uid = ?',[$data['buid']]);
        if($resb || $resu){
            //成功 转到列表页
            return redirect("/admin/block/index")->with('success','板块修改成功');
        }else{
            return back()->with('error','板块修改失败');
        }
    }
}
